/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.controller.admin;

import cn.edu.ntu.yyl.dao.ArticleDao;
import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleCategory;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.vo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class AdminArticle {
    @Autowired
    ArticleDao articleService;
    //删除文章
    @RequestMapping({"/deleteArticle.do"})
    public void DoDeleteArtic(@RequestBody int[] IdList){
        for (int i : IdList) {
            Article article = new Article();
            article.setId(i);
            articleService.DeleteArticle(article);
        }
    }
    //移至回收站
    @RequestMapping({"/articleStatusToTrash.do"})
    public void DoArticleStatusToTrash(@RequestBody int[] IdList){
        for (int i : IdList) {
            articleService.UpdateKeyByID("status","trash",i);
        }
    }
    //审核通过
    @RequestMapping({"/articleStatusToNormal.do"})
    public void DoArticleStatusToNormal(@RequestBody int[] IdList){
        for (int i : IdList) {
            articleService.UpdateKeyByID("status","normal",i);
        }
    }
    //新增博客
    public void AddArticle(HttpSession session,
                            HttpServletRequest request,
                            HttpServletResponse response,
                            Model model){

    }
    //编辑文章
    @PostMapping({"/articleWrite.do"})
    public void DoArticleWrite(@RequestBody ArticleVO articleVO){
        if (articleVO.getArticle().getId()>=1){
            //id大于等于1表示是修改
            Article article = articleVO.getArticle();
            ArticleCategory classify = articleVO.getClassify();
            List<ArticleCategory> tagList = new ArrayList<>();
            for (ArticleCategory tag : articleVO.getTags()) {
                tagList.add(tag);
            }
            UserInfo User = articleVO.getUserInfo();
            articleService.UpdateArticle(article);

        } else {
            //否则是添加
        }

    }
}
